<?php
/**
 * 用戶基础类
 * User: Administrator
 * Date: 2018\9\28 0028
 * Time: 15:36
 */

namespace app\Lib\Model\User;


use app\Lib\InterfaceIn\Role\RoleHandle;
use app\Lib\InterfaceIn\User\UserHandle;
use app\Lib\Model\DbModel\RoleUser;
use app\Lib\Model\DbModel\UserBase;
use app\Lib\Model\Role\Role;

class User implements UserHandle
{
    private $uid;
    private $cache;
    private $cachePrefix;
    private $cacheTime;
    private $Db ;
    private $roleCache = 'role_id';
    public function __construct($uid,$cache = true)
    {
        $this->uid = $uid;
        $this->cache = $cache;
        $this->cachePrefix = 'userBaseMessage';
        $this->cacheTime = 24*3600;
    }

    /**
     * 获取用户id
     * @return int
     */
    public function getUid()
    {
        return $this->uid;
    }

    /**
     * 用户账号
     * @return string
     */
    public function getAccount()
    {
        return $this->getFieldValue($this->getTable()->_account);
    }

    /**
     * 用户账号
     * @return string
     */
    public function getWeiXin()
    {
        return $this->getFieldValue($this->getTable()->_weixin);
    }

    /**
     * 用户角色类
     * @return RoleHandle |bool
     */
    public function getRoleClass()
    {
        $result = '';
        $cacheName = $this->getAllCacheName($this->roleCache);
        if($this->cache){
            $result = cache($cacheName);
        }
        if(empty($result)){
            $model  = (new RoleUser());
            $result = $model->where([$model->_uid=>$this->uid])->value($model->_role_id);
            $result = empty($result)?'null':$result;
            cache($cacheName,$result,$this->cacheTime);
        }
        $result = $result == 'null'?0:$result;
        return new Role($result);
    }

    /**
     * 获取用户权限类
     * @return mixed
     */
    public function getAuthClass()
    {
        // TODO: Implement getAuthClass() method.
    }

    /**
     * 获取值
     * @param string $field
     * @return mixed|string
     */
    private function getFieldValue($field = 'id'){
        //判断是否读库
        return $this->cache?$this->getCache($field):$this->getvalue($field);
    }

    /**
     * 获取缓存 || 生成缓存
     * @param string $cacheName
     * @return string
     */
    private function getCache($cacheName =''){
        if(empty($cacheName)){
            return '';
        }
        //获取缓存全名称
        $cacheNameAll = $this->getAllCacheName($cacheName);
        //获取缓存值
        $result = cache($cacheNameAll);
        //不存在生成缓存
        if(!$result){
            $result =  $this->getValue($cacheName);
            $result = empty($result) && $result!=0?null:$result;
            cache($cacheNameAll,$result,$this->cacheTime);
        }
        //返回值
        return $result;
    }

    /**
     * 获取缓存全称
     * @param $cacheName
     * @return string
     */
    private function getAllCacheName($cacheName){
        return  Md5($this->cachePrefix.'_'.$this->getUid().'_'.$cacheName);
    }
    /**
     * 数据库查询值
     * @param string $valueName
     * @return mixed
     */
    private function getValue($valueName = ''){
        $table = $this->getTable();
        $result = $table->where([$table->_id=>$this->uid])->value($valueName);
        return $result;
    }

    /**
     * 数据库
     * @return UserBase
     */
    private function getTable(){
        if(empty($this->Db)){
            $this->Db = new UserBase();
        }
        return $this->Db;
    }

    /**
     * 用户密码(加密)
     * @return string
     */
    public function getPassword()
    {
        $table = $this->getTable();
        return  $this->getFieldValue($table->_password);
    }

    /**
     * 获取账号状态
     * @return string
     */
    private function getAccountStatus(){
        return $this->getFieldValue($this->getTable()->_account_status);
    }

    /**
     * 账号是否被禁用
     * @return bool
     */
    public function isAllowLogin(){
        $result = $this->getAccountStatus();
        return $result == 0 || empty($result)?false:true;
    }

    /**
     * 获取账号名称
     * @return mixed|string
     */
    public function getNickName(){
        return $this->getFieldValue($this->getTable()->_nickname);
    }
    /**
     * 删除用户类所有缓存
     * @return bool
     */
    public function delUserCache()
    {
        //清除userBase缓存
        $array = $this->getTable()->getTableFields();
        foreach ($array as $v){
            $allCacheName = $this->getAllCacheName($v);
            cache($allCacheName,null);
        }
        //清除角色id缓存
        $allCacheName = $this->getAllCacheName($this->roleCache);
        cache($allCacheName,null);
        return true ;
    }

    static public function getWeiXinUid($openId){
        $model =  new UserBase();
        $where = [$model->_weixin=>$openId];
        $result =  $model->where($where)->value($model->_id);
        if(!$result){
            return false;
        }
        return $result;
    }
    static public function getAccountToUid($account){
        $model =  new UserBase();
        $where = [$model->_account=>$account];
        $result =  $model->where($where)->value($model->_id);
        if(!$result){
            return false;
        }
        return $result;
    }

    /**
     * 获取用户电话
     * @return string
     */
    public function getPhone()
    {
        return $this->getFieldValue($this->getTable()->_mobilephone);
    }

    /**
     * 获取用户最后登陆时间
     * @return string
     */
    public function getLastLoginTime()
    {
        return $this->getFieldValue($this->getTable()->_last_login_time);
    }

    /**
     * 获取性别
     * @return string
     */
    public function getSex()
    {
        $sex =  $this->getFieldValue($this->getTable()->_sex);
        return $sex;
    }

    /**
     * 性别中文
     * @return mixed
     */
    public function getSexStr()
    {
        $sex = $this->getSex();
        return $sex == 1?'男':'女';
    }

    /**
     * 获取生日
     * @return mixed
     */
    public function getBirthday()
    {
        $birthday=  $this->getFieldValue($this->getTable()->_birthday);
        return $birthday;
    }


}